<?php
/**
 * Product Dao definition
 *
 * @author fusq
 * @since 2009-08-15
 */

require_once BaseUtil::getModulePath("base") . '/models/BaseDao.php';

class ItemMetadataDao extends BaseDao{

    protected $_name;

    protected $productItem;

    protected $metadata;

    public function  _setup() {
        $this->_name = $this->prefix() . "item_metadatas";
        $this->productItem = $this->prefix() . "product_items";
        $this->metadata = $this->prefix() . "metadatas";
        parent::_setup();
    }

    public function insertItemMetadata($data){
        $this->insertData($data);
    }

    public function getDistinctMetadataValue($productId){
        $select = $this->db_r->select();
        $select->from($this->_name,array("distinct(metadata_id),value,id_value,desc_value"));
        $select->join($this->metadata,$this->_name.".metadata_id=" . $this->metadata . ".id",array("input_type","type"));
        $select->join($this->productItem,$this->_name.".product_item_id=" . $this->productItem . ".id",array("product_id"));
        $select->where($this->productItem . '.product_id = ?',$productId);
        $select->where($this->metadata . '.type = 1');
        $select->where($this->_name . '.is_delete = 0');
        $sql = $select->__toString();
        $result = $this->db_r->fetchAll($sql);
        return $result;
    }

    public function deleteItemMetadata($productItemId){
    	$isDel = array("is_delete"=>1);
    	$where = $this->db_w->quoteInto('product_item_id = ?', $productItemId);
    	$this->updateData($isDel, $where);
    }
    
    public function deleteItemMetadataByProductId($productId){
        $isDel = array("is_delete"=>1);
        $where = $this->db_w->quoteInto('product_item_id in (select id from product_items where product_id = ?) ', $productId);
        $this->updateData($isDel, $where);    	
    }
}